//
//  XiangXiViewController.m
//  XiangMu
//
//  Created by 康世标 on 15/8/13.
//  Copyright (c) 2015年 康世标. All rights reserved.
//

#import "XiangXiViewController.h"

@interface XiangXiViewController ()

@end

@implementation XiangXiViewController

- (void)viewDidLoad {
    [super viewDidLoad];
    
    self.navigationItem.title = @"订单详情";
    self.tableView1.backgroundColor=CL_efeff4;
    self.tableView1.separatorColor=CL_efeff4;
    // Do any additional setup after loading the view from its nib.
    [self.tableView1 registerNib:[UINib nibWithNibName:@"SexTableViewCell" bundle:nil] forCellReuseIdentifier:@"sex"];
    [self.tableView1 registerNib:[UINib nibWithNibName:@"FexTableViewCell" bundle:nil] forCellReuseIdentifier:@"fex"];
        [self.tableView1 registerNib:[UINib nibWithNibName:@"CexTableViewCell" bundle:nil] forCellReuseIdentifier:@"cex"];
    NSString *state=nil;
    [self.fuKuanBtn setBackgroundColor:CL_00cc85];
    [self.quXiaoDingBtn setBackgroundColor:CL_00cc85];
    
    if ([[self.dic valueForKey:@"state"] intValue]==0)
    {
        
        if ([[self.dic valueForKey:@"payWay"] intValue]==0)
        {
            self.xuYueBtn.hidden = YES;
            self.jieShuBtn.hidden =YES;
            [self.fuKuanBtn setTitle:@"付  款" forState:UIControlStateNormal];
            state = URL_DINGDANXIANGXIYIWANCHENG;

        }
        else{
            state = URL_DINGDANXIANGXIYIWANCHENG;
            self.xuYueBtn.hidden = YES;
            self.jieShuBtn.hidden =YES;
            self.fuKuanBtn.hidden = YES;
            self.quXiaoDingBtn.hidden = YES;
        }
           }
    else if ([[self.dic valueForKey:@"state"] intValue]==1){
        //        state = @"进行中";
        state = URL_DINGDANXIANGXIJINXINGZHONG;
        self.fuKuanBtn.hidden = YES;
        self.quXiaoDingBtn.hidden = YES;
    }
    else if ([[self.dic valueForKey:@"state"] intValue]==2){
        self.xuYueBtn.hidden = YES;
        self.jieShuBtn.hidden =YES;
        [self.fuKuanBtn setTitle:@"评  价" forState:UIControlStateNormal];
        self.quXiaoDingBtn.hidden = YES;
        state = URL_DINGDANXIANGXIYIWANCHENG;
    }
    else{
        state = URL_DINGDANXIANGXIYIWANCHENG;
        self.xuYueBtn.hidden = YES;
        self.jieShuBtn.hidden =YES;
        self.fuKuanBtn.hidden = YES;
        self.quXiaoDingBtn.hidden = YES;
    }
    

    
   self.orderId = [self.dic valueForKey:@"id"] ;
   self.url = state;
    
    [self QingQiu];
    [self mbporg];
}
- (void)mbporg{
    
   hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];
    
}
- (void)QingQiu{
    
    [QingQiuShuJu getwoDeDingDanXiangQing:self.url number:self.orderId Block:^(NSDictionary *dic, BOOL isOK) {
        
        if (isOK) {
            
            if ([[dic valueForKey:@"status"] intValue]==200) {
                
                self.datasDic = [dic valueForKey:@"data"];
                
                [self.tableView1 reloadData];
                [hud hide:YES];
                
            }
            else{
                
                hud.mode = MBProgressHUDModeText;
                hud.labelText = [dic valueForKey:@"msg"];
                [hud hide:YES afterDelay:2];
                
            }
        }
        else{
            hud.mode = MBProgressHUDModeText;
            hud.labelText = @"网络连接超时";
            [hud hide:YES afterDelay:2];

        }
        
    }];
}
#pragma mark---UITableViewDataSource
- (NSInteger)tableView:(UITableView *)tableView numberOfRowsInSection:(NSInteger)sectio{
    
    return 3;
}
- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (indexPath.row==0)
    {
        SexTableViewCell * cell=[tableView dequeueReusableCellWithIdentifier:@"sex" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
        cell.backgroundColor = CL_efeff4;
        
        [cell getCommen:[self.datasDic valueForKey:@"viewOrder"]];
        
        
        return cell;
    }
    else if(indexPath.row==1)
    {
        FexTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"fex" forIndexPath:indexPath];
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = CL_efeff4;
        [cell getReuest:self.dic];
//        cell.layout1.constant = 2;
            return cell;
    }
    else{
        CexTableViewCell * cell = [tableView dequeueReusableCellWithIdentifier:@"cex" forIndexPath:indexPath];
        cell.selectionStyle=UITableViewCellSelectionStyleNone;
     
        NSDictionary * dic =[self.datasDic valueForKey:@"carer"];
//        
        [cell getReuest:dic type:[self.datasDic valueForKey:@"viewOrder"]];
        


        return cell;
    }
    
}
- (CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    
    
    if (indexPath.row==0)
    {
        return 252;
    }
    else if (indexPath.row==1){
        return 143;
    }
    else{
        NSDictionary * dic =[self.datasDic valueForKey:@"carer"];
        
        
        CGRect  rect = [[dic valueForKey:@"description"] boundingRectWithSize:CGSizeMake(kScreenW-80-10, MAXFLOAT) options:NSStringDrawingUsesLineFragmentOrigin attributes:@{ NSFontAttributeName :[UIFont systemFontOfSize:13]} context:nil];
        
        return rect.size.height + 150;
      
        
    }
}
#pragma mark---UITableViewDelegate

#pragma mark -- 提前结束
- (IBAction)tiQianjieShu:(UIButton *)sender{
    
    UIAlertView * alertView =[[UIAlertView alloc]initWithTitle:nil message:@"请输入支付宝账号" delegate:self cancelButtonTitle:@"确定" otherButtonTitles:@"取消", nil];
    
    [alertView setAlertViewStyle:UIAlertViewStylePlainTextInput];
    
    UITextField * textField =[alertView textFieldAtIndex:0];
    
    textField.placeholder = @"请输入您的退款账号";
    
    
    [alertView show];
    
    
}
#pragma mark -- 续约订单
- (IBAction)xuYueDingDan:(UIButton *)sender{
    
    NSDictionary * dic1 = [self.datasDic valueForKey:@"carer"];
    
    XiaDanViewController * xia =[[XiaDanViewController alloc]init];
    
    xia.dic =dic1;
    
    [self.navigationController pushViewController:xia animated:YES];
}
#pragma mark---付款
- (IBAction)fuKuan:(id)sender{
    if ([[self.dic valueForKey:@"state"] intValue]==0)
    {
        //        state = @"未付款";
        TiJiaoDingDanViewController * t = [[TiJiaoDingDanViewController alloc]init];
        NSDictionary * dic1 = [self.datasDic valueForKey:@"viewOrder"];
        
        t.woDeDingDan = YES;
        
        t.dataDic =@{@"order":dic1};
        
        [self.navigationController pushViewController:t animated:YES];
    }
    else if ([[self.dic valueForKey:@"state"] intValue]==1){
        //        state = @"进行中";

    }
    else if ([[self.dic valueForKey:@"state"] intValue]==2){
        
        PingJiaViewController * ping = [[PingJiaViewController alloc]init];
        NSDictionary * dic1 = [self.datasDic valueForKey:@"viewOrder"];
        
        ping.orderId =[dic1 valueForKey:@"id"] ;
        

        [self.navigationController pushViewController:ping
                                             animated:YES];
        
    }
    else{

    }
    

   
    
    
}
#pragma mark---取消订单
- (IBAction)quXiao:(id)sender{
    
    hud = [MBProgressHUD showHUDAddedTo:self.view animated:YES ];
    
    hud.labelText = @"正在取消订单";
    
    NSDictionary * dic1 = [self.datasDic valueForKey:@"viewOrder"];
    [QingQiuShuJu weiZhiFuDingDanQuXiao:[dic1 valueForKey:@"id"] blcok:^(NSDictionary *dic, BOOL isOK) {
        
        if (isOK) {
            
            hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
            
            hud.mode = MBProgressHUDModeCustomView;
            
            hud.labelText = @"订单已取消";
            
            [hud hide:YES afterDelay:2];
            
            [self performSelector:@selector(fanhui) withObject:self afterDelay:2];
            
        }
        else{
            hud.mode = MBProgressHUDModeText;
            
            hud.labelText = @"网络连接错误";
            
            [hud hide:YES afterDelay:2];
        }
    
        hud.removeFromSuperViewOnHide = YES;
    }];
}
- (void)fanhui{
    
    [self.navigationController popViewControllerAnimated:YES];
}
#pragma mark --- UIAlertViewDelegate
- (void)alertView:(UIAlertView *)alertView clickedButtonAtIndex:(NSInteger)buttonIndex{
    
    UITextField * alertTextFiel = [alertView textFieldAtIndex:0];
  
    switch (buttonIndex) {
        case 0:
        {  hud =[MBProgressHUD showHUDAddedTo:self.view animated:YES];

            if ([alertTextFiel.text isEqualToString:@""]||alertTextFiel.text==nil)
            {
                hud.mode = MBProgressHUDModeText;
                
                hud.labelText = @"请输入支付宝账号";
                [hud hide:YES afterDelay:2];
                
            }
            else{
                
                
                NSDictionary * dic1 = [self.datasDic valueForKey:@"viewOrder"];
                
                
                
                NSString * token =[[NSUserDefaults standardUserDefaults]objectForKey:@"msg"];
                
                [QingQiuShuJu tiQianJieShu:@{@"orderId":[dic1 valueForKey:@"id"],@"alipayNum":alertTextFiel.text,@"ticket":token} block:^(NSDictionary *dic, BOOL isOK)
                 {
                     
                     if (isOK)
                     {
                         
                         
                         if ([[dic valueForKey:@"status"] intValue]==200) {
                             
                             hud.customView = [[UIImageView alloc] initWithImage:[UIImage imageNamed:@"37x-Checkmark.png"]];
                             hud.mode = MBProgressHUDModeCustomView;
                             hud.labelText =  [dic valueForKey:@"msg"];
                             [hud hide:YES afterDelay:2];
                             
                         }
                         else
                         {
                             hud.mode = MBProgressHUDModeText;
                             
                             hud.labelText =  [dic valueForKey:@"msg"];
                             [hud hide:YES afterDelay:2];
                             
                         }
                     }
                     else{
                         hud.mode = MBProgressHUDModeText;
                         
                         hud.labelText =  [dic valueForKey:@"网络连接错误"];
                         [hud hide:YES afterDelay:2];
                     }
                 }];
                

            }
 
        
        }
            break;
        case 1:
        {
            
        }
            break;
        default:
            break;
    }
}
- (void)didReceiveMemoryWarning {
    [super didReceiveMemoryWarning];
    // Dispose of any resources that can be recreated.
}


/*
#pragma mark - Navigation

// In a storyboard-based application, you will often want to do a little preparation before navigation
- (void)prepareForSegue:(UIStoryboardSegue *)segue sender:(id)sender {
    // Get the new view controller using [segue destinationViewController].
    // Pass the selected object to the new view controller.
}
*/

@end
